package com.zjhb.common.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.jbarcode.JBarcode;
import org.jbarcode.JBarcodeFactory;
import org.jbarcode.encode.Code128Encoder;
import org.jbarcode.encode.InvalidAtributeException;
import org.jbarcode.paint.TextPainter;
import org.jbarcode.util.ImageUtil;

/**
 * 生成自定义条形码
 * 
 * @author zj
 *
 */
public class JbaRcode {

	private static final int BARCODE_HEIGHT = 45;

	private static final int BARCODE_DPI = ImageUtil.DEFAULT_DPI;

	private static final String FONT_FAMILY = "console";

	private static final int FONT_SIZE = 10;

	public static String TEXT = "";

	private static JBarcode jbc = null;

	static JBarcode getJBarcode() throws InvalidAtributeException {
		if (jbc == null) {
			// 生成code128
			jbc = JBarcodeFactory.getInstance().createCode128();
			jbc.setEncoder(Code128Encoder.getInstance());
			jbc.setTextPainter(CustomTextPainter.getInstance());
			jbc.setBarHeight(BARCODE_HEIGHT);
			jbc.setXDimension(Double.valueOf(0.8).doubleValue());
			jbc.setShowText(true);
		}
		return jbc;
	}

	public static String createBarcode(String message, String text) {
		try {
			File file = new File("C:\\upload\\codeImg");
			if (!file.exists()) {
				file.mkdirs();
			}
			String fileName = file + "/" + message + ".png";
			FileOutputStream fos = new FileOutputStream(fileName);
			createBarcode(message, fos, text);
			fos.close();
			return fileName;
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static void createBarcode(String message, OutputStream os, String text) {
		try {
			// 设置条形码文本
			TEXT = text;
			// 创建条形码的BufferedImage图像
			BufferedImage image = getJBarcode().createBarcode(message);
			ImageUtil.encodeAndWrite(image, ImageUtil.PNG, os, BARCODE_DPI, BARCODE_DPI);
			os.flush();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	protected static class CustomTextPainter implements TextPainter {
		private static CustomTextPainter instance = new CustomTextPainter();

		public static CustomTextPainter getInstance() {
			return instance;
		}

		public void paintText(BufferedImage barCodeImage, String text, int width) {
			// 绘图
			Graphics g2d = barCodeImage.getGraphics();
			// 创建字体
			Font font = new Font(FONT_FAMILY, Font.PLAIN, FONT_SIZE * width);
			g2d.setFont(font);
			FontMetrics fm = g2d.getFontMetrics();
			int height = fm.getHeight();
			int center = (barCodeImage.getWidth() - fm.stringWidth(text)) / 2;
			g2d.setColor(Color.WHITE);
			g2d.fillRect(0, 0, barCodeImage.getWidth(), barCodeImage.getHeight() * 3 / 20);
			g2d.fillRect(0, barCodeImage.getHeight() - (height * 9 / 10), barCodeImage.getWidth(), (height * 9 / 10));
			g2d.setColor(Color.BLACK);
			// 绘制条形码
			g2d.drawString(text, center , barCodeImage.getHeight() - (height / 10) - 2);
		}
	}

}
